﻿using NDK.Motion.MachineResources;
using System;
using System.Linq;
using System.Windows.Forms;

namespace NDK.Motion.MachineManager
{
    public partial class Frm_AddCylinder : Form
    {
        private static log4net.ILog traceLogger = log4net.LogManager.GetLogger("Frm_AddCylinder");
        public Frm_AddCylinder()
        {
            InitializeComponent();

            //初始化界面下拉框
            foreach (var diModel in MachineDataManager.Instance.DIList)
            {
                cb_Input_Home.Items.Add(diModel.Name);
                cb_Input_Work.Items.Add(diModel.Name);
            }
            foreach (var doModel in MachineDataManager.Instance.DOList)
            {
                cb_Output_Home.Items.Add(doModel.Name);
                cb_Output_Work.Items.Add(doModel.Name);
            }
        }

        private void btn_Confirm_BtnClick(object sender, EventArgs e)
        {
            //气缸的名称不能为空
            string name = tb_Name.Text.Trim();
            if (string.IsNullOrWhiteSpace(name)) return;
            //气缸的反馈sensor不能都为空
            InputSetting input_Home = MachineDataManager.Instance.DIList.SingleOrDefault(i => i.Name == cb_Input_Home.Text);
            InputSetting input_Work = MachineDataManager.Instance.DIList.SingleOrDefault(i => i.Name == cb_Input_Work.Text);
            if (input_Home == null && input_Work == null)
            {
                return;
            }
            //气缸的控制端口，不能都为空
            OutputSetting output_Home = MachineDataManager.Instance.DOList.SingleOrDefault(i => i.Name == cb_Output_Home.Text);
            OutputSetting output_Work = MachineDataManager.Instance.DOList.SingleOrDefault(i => i.Name == cb_Output_Work.Text);
            if (output_Home == null && output_Work == null)
            {
                return;
            }

            if (cb_Type.Text == "单控")
            {
                SingleDriveCylinder singleDriveCylinder = new SingleDriveCylinder(name, output_Home, input_Home, input_Work);
                MachineDataManager.Instance.CylinderList.Add(singleDriveCylinder);
            }
            if (cb_Type.Text == "双控")
            {
                DoubleDriveCylinder doubleDriveCylinder = new DoubleDriveCylinder(name, output_Home, output_Work, input_Home, input_Work);
                MachineDataManager.Instance.CylinderList.Add(doubleDriveCylinder);
            }
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_Cancel_BtnClick(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void cb_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_Type.Text == "单控")
            {
                lbl_Work.Visible = false;
                cb_Output_Work.Visible = false;
                lbl_Home.Text = "Home/Work";
            }
            if (cb_Type.Text == "双控")
            {
                lbl_Work.Visible = true;
                cb_Output_Work.Visible = true;
                lbl_Home.Text = "Home";
            }
        }
    }
}
